package com.rem.concurrency.multithreading.syn;

import java.util.ArrayList;
import java.util.List;

/**
 * 线程不安全的list
 *
 * @author Rem
 * @date 2021-03-05
 */

public class UnsafeList {

    public static void main(String[] args) throws InterruptedException {

        List<String> list = new ArrayList<>();

        //list的大小10000
       /* for (int i = 0; i < 10000; i++) {
            list.add(String.valueOf(i));
        }*/


        //list的大小9790 不是10000
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                list.add(Thread.currentThread().getName());

            }).start();
        }

        //主线程休眠下 让其他线程先跑完 就有10000
        Thread.sleep(1000);


        System.err.println(list.size());
    }
}

